package cn.xz.tipsum.whight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 作者:zch
 * 时间:2022/5/6 9:58
 * 描述:圆盘形颜色选择器
 */
public class YColorView extends View {

    private int[] c = null;
    private final int[] m_c = {Color.parseColor("#1E90FF"),Color.parseColor("#228B22"),Color.BLUE,Color.RED,Color.GREEN,Color.BLACK,Color.GRAY,Color.YELLOW,Color.WHITE,Color.MAGENTA,Color.LTGRAY,Color.BLUE,Color.parseColor("#1E90FF")};

    public YColorView(Context context) {
        super(context);
    }

    public YColorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    //动态设置颜色
    public void setColor(int[] i){
        this.c = i;
        //刷新，此方法无需在UI线程使用
        postInvalidate();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //定义画笔
        Paint paint = new Paint();
        //防锯齿
        paint.setAntiAlias(true);
        //获取宽度
        int measuredWidth = getMeasuredWidth();
        //获取高度
        int measuredHeight = getMeasuredHeight();
        //取最小的为半径，防止长方形
        int length = Math.min(measuredWidth, measuredHeight);
        //设置风格-实心圆
        paint.setStyle(Paint.Style.FILL);

        //自定义颜色的情况下，否则使用默认
        RectF rectF;
        if (c != null && c.length > 0){
            //一个圆 360 度,需要多少颜色，就将这些颜色在 360内均分
            int l = 360 / c.length;
            for (int i=0; i< c.length; i++){
                //设置一个圆弧以及范围，虽然是一个圆弧，也要按照圆来设置左上右下的范围
                rectF = new RectF(0,0,length,length);
                //设置当前画笔颜色
                paint.setColor(c[i]);
                //将画笔与圆弧搞在画板里面，起始位置要按当前下标的位置计算，弧度固定均分360度
                canvas.drawArc(rectF, i * l, l,true, paint);
            }
        }else {
            //设置一个圆弧以及范围，虽然是一个圆弧，也要按照圆来设置左上右下的范围
            rectF = new RectF(0,0,length,length);
            //指定扫射圆心 cx, cy
            //指定渐变色色值数组
            //对应色值数组，用来控制对应色值的权重，如为 null，就按系统的均匀划分吧
            SweepGradient sweepGraditent = new SweepGradient(length/2f,length/2f,m_c,null);
            //确定好渐变色后，只要把这个渐变色效果设置给画笔，画笔就能带着颜色画出来了
            paint.setShader(sweepGraditent);
            canvas.drawArc(rectF, 0, 360, false, paint);
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //重新焦点方法，用于在移动以及点击时及时获取颜色
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                //获取当前位置的颜色，通过回调及时返回数据,这里加限制，否则手指到指定范围会闪退
                if (event.getX() < getMeasuredWidth() && event.getY() < getMeasuredHeight()){
                    int pixel = createBitmapFromView(this).getPixel((int) Math.abs(event.getX()), (int) Math.abs(event.getY()));
                    onYColorViewInterface.onYColorChange(pixel);
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    public interface OnYColorViewInterface{
        void onYColorChange(int color);
    }

    public OnYColorViewInterface onYColorViewInterface;

    public void addOnYColorListener(OnYColorViewInterface onYColorViewInterface){
        this.onYColorViewInterface = onYColorViewInterface;
    }

    //将该View转换成一个图像，用于根据坐标获取当前图像的颜色
    public static Bitmap createBitmapFromView(View view) {
        Bitmap bitmap = null;
        //开启view缓存bitmap
        view.setDrawingCacheEnabled(true);
        //设置view缓存Bitmap质量
        view.setDrawingCacheQuality(DRAWING_CACHE_QUALITY_HIGH);
        //获取缓存的bitmap
        Bitmap cache = view.getDrawingCache();
        if (cache != null && !cache.isRecycled()) {
            bitmap = Bitmap.createBitmap(cache);
        }
        //销毁view缓存bitmap
        view.destroyDrawingCache();
        //关闭view缓存bitmap
        view.setDrawingCacheEnabled(false);
        return bitmap;
    }
}

